package com.zjb.home.boxingtu.view.circle;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import com.xinyartech.baselibrary.utils.LogUtil;
import com.zjb.home.boxingtu.R;
import com.zjb.home.boxingtu.view.BaseView;

public class QingChuangLiveDateTextView extends BaseView {

    private Paint paint;
    private PathMeasure pathMeasure01;
    private PathMeasure pathMeasure02;
    private PathMeasure pathMeasure03;
    float du;
    private Path dst01;
    private Path path01;
    private Path path02;
    private Path path03;
    private Path dst02;
    private String text = "89号被查看次数最高";
    private Rect rect;
    private Path dst03;

    public QingChuangLiveDateTextView(@NonNull Context context) {
        super(context);
    }

    public QingChuangLiveDateTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public QingChuangLiveDateTextView(@NonNull Context context, @Nullable AttributeSet attrs,
                                      int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initAttrs(AttributeSet attrs) {

    }

    @Override
    public void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#F928AF"));
        paint.setStrokeWidth(getResources().getDimension(R.dimen.m0_5));
        paint.setTextSize(getResources().getDimension(R.dimen.m13));
        pathMeasure01 = new PathMeasure();
        pathMeasure02 = new PathMeasure();
        pathMeasure03 = new PathMeasure();
        path01 = new Path();
        path02 = new Path();
        path03 = new Path();
        dst01 = new Path();
        dst02 = new Path();
        dst03 = new Path();
        rect = new Rect();
    }

    {
        start();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path01.reset();
        path01.moveTo(0, getHeight());
        path01.lineTo(getResources().getDimension(R.dimen.m5),
                getResources().getDimension(R.dimen.m10));
        path01.lineTo(getResources().getDimension(R.dimen.m30),
                getResources().getDimension(R.dimen.m10));
        pathMeasure01.setPath(path01, false);

        path02.reset();
        path02.addCircle(getResources().getDimension(R.dimen.m30),
                getResources().getDimension(R.dimen.m10), getResources().getDimension(R.dimen.m1_5)
                , Path.Direction.CW);
        pathMeasure02.setPath(path02, false);

        if (du <= 1f) {
            paint.setStyle(Paint.Style.STROKE);
            canvas.save();
            dst01.reset();
            pathMeasure01.getSegment(0,
                    (pathMeasure01.getLength()) * du, dst01, true);
            canvas.drawPath(dst01, paint);
            canvas.restore();
        } else if (du <= 1.5f) {
            paint.setStyle(Paint.Style.STROKE);
            canvas.save();
            dst01.reset();
            pathMeasure01.getSegment(0,
                    (pathMeasure01.getLength()) * 1f, dst01, true);
            canvas.drawPath(dst01, paint);
            canvas.restore();

            paint.setStyle(Paint.Style.FILL);
            canvas.save();
            dst02.reset();
            pathMeasure02.getSegment(0,
                    (pathMeasure02.getLength()) * (du - 1f),
                    dst02, true);
            canvas.drawPath(dst02, paint);
            canvas.restore();
        } else {
            paint.setStyle(Paint.Style.STROKE);
            canvas.save();
            dst01.reset();
            pathMeasure01.getSegment(0,
                    (pathMeasure01.getLength()) * 1f, dst01, true);
            canvas.drawPath(dst01, paint);
            canvas.restore();

            paint.setStyle(Paint.Style.FILL);
            canvas.save();
            dst02.reset();
            pathMeasure02.getSegment(0,
                    (pathMeasure02.getLength()) * 2f,
                    dst02, true);
            canvas.drawPath(dst02, paint);
            canvas.restore();


            paint.getTextBounds(text, 0, text.length(), rect);
            path03.reset();
            path03.moveTo(getResources().getDimension(R.dimen.m35),
                    getResources().getDimension(R.dimen.m10) + rect.height() / 2f - getResources().getDimension(R.dimen.m2));
            path03.addRect(getResources().getDimension(R.dimen.m35) + rect.width() * (du - 1.5f),
                    getResources().getDimension(R.dimen.m10) - rect.height() / 2f,
                    getResources().getDimension(R.dimen.m35) + rect.width(),
                    getResources().getDimension(R.dimen.m10) + rect.height() / 2f,
                    Path.Direction.CCW);
//            paint.getTextPath(text, 0, text.length(), getResources().getDimension(R.dimen.m35),
//                    getResources().getDimension(R.dimen.m10) + rect.height() / 2f -
//                    getResources().getDimension(R.dimen.m2), path03);
//            pathMeasure03.setPath(path03, false);

            path03.setFillType(Path.FillType.INVERSE_EVEN_ODD);
            canvas.clipPath(path03);


            canvas.drawText(text, getResources().getDimension(R.dimen.m35),
                    getResources().getDimension(R.dimen.m10) + rect.height() / 2f - getResources().getDimension(R.dimen.m2), paint);
        }
    }

    public void setDu(float du) {
        this.du = du;
        invalidate();
    }

    public void start() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "du", 0, 2.5f);
        animator.setDuration(6000);
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.start();
    }
}
